package com.boot2.core.dao.datasource;

import lombok.extern.apachecommons.CommonsLog;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

/**
 * @author zhangweilin
 * @Description: 动态切换数据源
 * @date 2018/11/2
 */
@Aspect
@Component
@CommonsLog
@ConditionalOnProperty("spring.datasource.customDefaultName")
//@Conditional(DynamicDatasourceAopCondition.class)
//@ConditionalOnMissingBean(AbstractDynamicDatasourceAop.class)
public class DynamicDatasourceAop extends AbstractDynamicDatasourceAop implements Ordered {
    public static final String instanceName = "dynamicDatasourceAop";

    public DynamicDatasourceAop() {
        System.out.println("动态数据源初始化");
    }

    /**
     * 定义有一个切入点，范围为web包下的类
     * 动态修改，参数https://blog.csdn.net/qq_15396517/article/details/84106997
     */
//    @Pointcut("execution(* com..*.dao..*.*(..)) && !execution(* com..*.redis..*.*(..))")
    @Pointcut("(execution(* com..*.service..*.*(..)) || execution(* com..*.dao..*.*(..))) && !execution(* com..*.redis..*.*(..))")
//    @Pointcut("@annotation(com.boot2.core.dao.datasource.DataSource)")
    public void checkParam() {
    }


    /**
     * Get the order value of this object.
     * <p>Higher values are interpreted as lower priority. As a consequence,
     * the object with the lowest value has the highest priority (somewhat
     * analogous to Servlet {@code load-on-startup} values).
     * <p>Same order values will result in arbitrary sort positions for the
     * affected objects.
     *
     * @return the order value
     * @see #HIGHEST_PRECEDENCE
     * @see #LOWEST_PRECEDENCE
     */
    @Override
    public int getOrder() {
        return 10;
    }
}
